package com.bookcycle.panda.webfront.controller;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.security.token.SSOToken;
import com.baomidou.kisso.web.waf.request.WafRequestWrapper;
import com.bookcycle.panda.webfront.constant.URLConstant;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @author Mason
 * @date 2018/3/6
 */
@Controller
public class LoginController extends BaseController {


    @Login(action = Action.Skip)
    @RequestMapping("/loginPost")
    public String loginPost(@RequestParam("account")String account,
                            @RequestParam("password")String password,
                                                        Model model){
        WafRequestWrapper requestWrapper=new WafRequestWrapper(request);
        RestTemplate restTemplate=new RestTemplate();
        MultiValueMap<String,Object> param=new LinkedMultiValueMap<>();
        param.add("account",account);
        param.add("password",password);
        ResponseEntity<String> responseEntity=restTemplate.postForEntity(URLConstant.URL_LOGIN_BY_ACCOUNT,param,String.class);
        String userId=responseEntity.getBody();
        if(null != userId&&!"".equals(userId)){
            SSOHelper.setCookie(request,response, SSOToken.create().setId(userId),true);
        }
        return "home";
    }
}
